# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\worker\settings_storage\__init__.py
from threading import RLock
import json


class SettingsStorage:
    def __init__(self, file_name):
        self._lock = RLock()
        self.file_name = file_name
        self._data = None

    @property
    def data(self):
        with self._lock:
            if self._data is None:
                try:
                    self._data = json.load(open(self.file_name, "rt", encoding="utf-8"))
                except FileNotFoundError:
                    self._data = dict()

                return self._data

    def get(self, name, default=None):
        return self.data.get(name, default)

    def delete(self, name):
        with self._lock:
            if name in self._data:
                del self._data[name]
                json.dump(self._data, open(self.file_name, "wt", encoding="utf-8"))

    def set(self, name, value):
        with self._lock:
            if (self.get(name, default=None)) != value:
                self._data[name] = value
                json.dump(self._data, open(self.file_name, "wt", encoding="utf-8"))
